package com.javabi.htmlbuilder.html.element.table;

import java.util.ArrayList;
import java.util.List;

import com.javabi.htmlbuilder.html.HTMLBuilder;
import com.javabi.htmlbuilder.html.HTMLElement;
import com.javabi.htmlbuilder.html.HTMLElementName;

/**
 * An HTML Table Row Element.
 * <p>
 * &lt;tr&gt; ... &lt;/tr&gt;
 * </p>
 */
public class Row extends HTMLElement<Row> {

	/** The cell list. */
	private final List<Cell> cellList = new ArrayList<Cell>();

	/**
	 * Returns a new table cell.
	 * @return a new table cell.
	 */
	public Cell td() {
		Cell cell = new Cell();
		cellList.add(cell);
		return cell;
	}

	/**
	 * Returns a new table cell.
	 * @param rowspan the cell row span.
	 * @param colspan the cell column span.
	 * @return a new table cell.
	 */
	public Cell td(int rowspan, int colspan) {
		Cell cell = td();
		cell.setRowspan(rowspan);
		cell.setColspan(colspan);
		return cell;
	}

	/**
	 * Returns a new table cell.
	 * @return a new table cell.
	 */
	public Cell cell() {
		return td();
	}

	/**
	 * Returns a new table cell.
	 * @param rowspan the cell row span.
	 * @param colspan the cell column span.
	 * @return a new table cell.
	 */
	public Cell cell(int rowspan, int colspan) {
		return td(rowspan, colspan);
	}

	/**
	 * Returns an existing table cell.
	 * @param index the index.
	 * @return an existing table cell.
	 */
	public Cell getCell(int index) {
		return cellList.get(index);
	}

	/**
	 * Returns true to format this element.
	 * @return true to format this element.
	 */
	public boolean format() {
		for (Cell cell : cellList) {
			if (cell.format()) {
				return true;
			}
		}
		return false;
	}

	/**
	 * Returns the element name.
	 * @return the element name.
	 */
	public HTMLElementName getElementName() {
		return HTMLElementName.TABLE_ROW;
	}

	/**
	 * Append this to the given html builder.
	 * @param html the html builder.
	 * @return the html builder.
	 */
	public HTMLBuilder appendTo(HTMLBuilder html) {
		html.open(this, true);
		appendInlineStyleAttributesTo(html);
		for (Cell cell : cellList) {
			cell.appendTo(html);
		}
		html.close(this);
		return html;
	}

}
